package org.dandsoft.blackbox.labs.snn;

import java.io.Reader;
import java.io.IOException;

/**
 * User: dvilyuzhanin
 * Date: 23.01.2009 15:16:30
 */
@Deprecated
public class NeuronStructureReader {

    private Reader reader;
    private boolean isNewLayer = true;
    private int layerNumber = -1;
    private int layerSize = -1;
    private int synapsesSize = 0;
    private int neuronNumber = 0;
    private int[] destinationNumbers;
    private boolean isStreamFinished;

    public NeuronStructureReader(Reader reader) {
        this.reader = reader;
    }


    public void readNext() throws IOException {
        if (!isNewLayer) {
            neuronNumber++;
            destinationNumbers = readArray();

        } else {
            neuronNumber = 0;
            destinationNumbers = null;
        }
    }

    public boolean nextLayer() throws IOException {
        try {
            if (isNewLayer) {
                layerNumber = Integer.parseInt(readUntil("" + NeuronStructureWriter.SEPARATOR));
                layerSize = Integer.parseInt(readUntil("" + NeuronStructureWriter.SEPARATOR));
                synapsesSize = Integer.parseInt(readUntil("" + NeuronStructureWriter.SEPARATOR));
                isNewLayer = false;

            }
            return true;
        } catch (RuntimeException e) {
            //e.printStackTrace();
            isStreamFinished = true;
            return false;
        }

    }

    private String readUntil(String stopPattern) throws IOException {
        StringBuilder builder = new StringBuilder();
        int ch;
        while (((ch = reader.read()) != -1) && (stopPattern.indexOf(ch) == -1)) {
            if (ch == '\n') {
                isNewLayer = true;
                break;
            } else {
                builder.append((char) ch);
            }

        }
        String result = builder.toString();
        if (ch == -1) {
            isStreamFinished = true;
            if (result.length() == 0) {
                //TODO: тут костыль
                throw new RuntimeException();
            }
        }
        return result;
    }

    private int[] readArray() throws IOException {
        int size = Integer.parseInt(readUntil("" + NeuronStructureWriter.SIZE_SEPARATOR));
        int[] result = new int[size];
        int ch;
        for (int i = 0; i < result.length; i++) {
            int value = Integer.parseInt(readUntil(NeuronStructureWriter.VALUE_SEPARATOR + "" +
                                                   NeuronStructureWriter.SEPARATOR));
            result[i] = value;
        }

        return result;
    }

    public int getLayerNumber() {
        return layerNumber;
    }

    public int getLayerSize() {
        return layerSize;
    }

    public int getNeuronNumber() {
        return neuronNumber;
    }

    public int[] getDestinationNumbers() {
        return destinationNumbers;
    }

    public int getSynapsesSize() {
        return synapsesSize;
    }
}
